﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entidades;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

namespace Datos
{
   public  class RecaudacionAD
    {


        public static int NuevaRecaudacion(Recaudacion recaudacion)
        {
            OracleConnection oraConn = new OracleConnection();
            int k = 0;
            string connString = "Server=XE;Uid=juan;Pwd=lagartija";
            if (oraConn.State != ConnectionState.Open)
            {
                try
                {
                    oraConn.ConnectionString = connString;

                    oraConn.Open();

                    Console.WriteLine(oraConn.ConnectionString, "Successful Connection");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message, "Exception Caught");
                }
            }

            if (oraConn.State == ConnectionState.Open)
            {
                string sqlInsert = "insert into recaudacion ";
                sqlInsert += "values (sequence_recaudacion.nextval, :p_fecha, :p_monto_recaudado, :p_monto_por_recaudar)";

                OracleCommand cmdInsert = new OracleCommand();
                cmdInsert.CommandText = sqlInsert;
                cmdInsert.Connection = oraConn;

                OracleParameter pFecha = new OracleParameter();
                pFecha.Value = recaudacion.fecha;
                pFecha.ParameterName = "p_fecha";

                OracleParameter pMontoRecaudado = new OracleParameter();
                pMontoRecaudado.DbType = DbType.Int32;
                pMontoRecaudado.Value = Convert.ToInt32(recaudacion.MontoRecaudado);
                pMontoRecaudado.ParameterName = "p_monto_recaudado";

                OracleParameter pMontoPorRecaudar = new OracleParameter();
                pMontoRecaudado.DbType = DbType.Int32;
                pMontoPorRecaudar.Value = recaudacion.MontoPorRecaudar;
                pMontoPorRecaudar.ParameterName = "p_monto_por_recaudar";

           





                cmdInsert.Parameters.Add(pFecha);
                cmdInsert.Parameters.Add(pMontoRecaudado);
                cmdInsert.Parameters.Add(pMontoPorRecaudar);
              



                k = cmdInsert.ExecuteNonQuery();

                cmdInsert.Dispose();







            } return k;
        }

        public static bool UltimaRecaudacion(string fecha)
        {
            string sql = @"select	 *
	                        from   	 RECAUDACION 
	                        where    FECHA = '"+fecha+"'";




            OracleConnection Oraclecon = new OracleConnection("Server=XE;Uid=juan;Pwd=lagartija");
            Oraclecon.Open();
            OracleCommand myCMD = new OracleCommand();
            myCMD.Connection = Oraclecon;
            myCMD.CommandText = sql;
            myCMD.CommandType = CommandType.Text;




            int count = Convert.ToInt32(myCMD.ExecuteScalar());

            if (count == 0)
                return false;
            else
                return true;
        }
    }
}
